// pages/buy/buy.js

const app = getApp()
const db = wx.cloud.database()
const _ = db.command



Page({

  /**
   * 页面的初始数据
   */
  data: {
    concertId: [],
    concertInfo: [],
    concerts: [],
    time: "",
    location: "",
    title: "",
    img_url: "",
    price: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var tempId = options.concertId;
    this.concertId = options.concertId
    this.setData({
      concertId: tempId
    })
    console.log(this.data.concertId + 'aaas');
    db.collection('concert').get({
      success: res => {
        this.setData({
          concerts: res.data
        })
        console.log("a")
        var a = this.data.concerts.length;
        console.log(a)
        for (var i = 0; i < a; i++) {
          var k = this.data.concerts[i];
          if (this.data.concertId == k._id) {
            this.data.concertInfo.push(k);
            console.log("aha");
            this.setData({
              title: k.name,
              location: k.place,
              time: k.date,
              img_url: k.cover_url,
              price: k.price
            })
            console.log(this.data.concertInfo);
          }
        }
      }
    })
  },

  onTapGetTicket: function() {
    wx.showLoading({
      title: '取票中...',
      mask: true
    })
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        this.openid = res.result.openid
        db.collection('verifiedperson').where({
          _userid: this.openid // 填入当前用户 openid
        }).get({
          success: res => {
            if (Object.keys(res.data).length == 0) {
              wx.hideLoading()
              wx.showModal({
                title: '提示',
                content: '领票前需要录入人脸，请点击确认录入',
                success: function(res) {
                  console.log(res)
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '../entry/entry',
                    })
                  } else {
                    wx.hideLoading()
                    wx.showToast({
                      title: '需要录入人脸可以在"我的"页面中录入人脸',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                }
              })
            } else {
              // wx.showLoading({
              //   title: '取票中...',
              //   mask: true
              // })
              console.log(this.concertId)
              wx.cloud.callFunction({
                name: 'Add_GotticketsCollect',
                data: {
                  userid: this.openid,
                  concertid: this.concertId
                },
                success: res => {
                  wx.cloud.callFunction({
                    name: 'Add_TicketsCollect',
                    data: {
                      userid: this.openid,
                      concertid: this.concertId
                    },
                    success: res => {
                      console.log(res.data)
                      wx.hideLoading()
                      wx.showToast({
                        title: '成功取票',
                        icon: 'success',
                        duration: 2000,
                        mask: true
                      })
                      app.globalData.hadBought[this.concertId] = 1
                      setTimeout(function() {
                        wx.navigateBack()
                      }, 1500)
                    },
                    fail: err => {
                      console.log(err)
                    }
                  })
                },
                fail: err => {
                  console.log(err)
                }
              })
            }
          },
          fail: err => {
            console.log(err)
          }
        })
      },
      fail: err => {
        console.error(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})